package com.resource.coursemanage.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.common.utils.AppResponse;
import com.resource.common.utils.R;
import com.resource.coursemanage.entity.TCourseInformationEntity;
import com.resource.coursemanage.service.TCourseInformationService;
import com.resource.coursemanage.utils.TimeJudgeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.resource.common.utils.PageUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 课程信息表
 *
 * @author zkx
 * @email 1486664603@qq.com
 * @date 2023-03-06 20:53:55
 */
@RestController
@RequestMapping("course/tcourseinformation")
public class TCourseInformationController {
    @Autowired
    private TCourseInformationService tCourseInformationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tCourseInformationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TCourseInformationEntity tCourseInformation = tCourseInformationService.getById(id);

        return R.ok().put("tCourseInformation", tCourseInformation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TCourseInformationEntity tCourseInformation) throws ParseException {
        //课程开始时间
//        String courseStartTime = tCourseInformation.getCourseStartTime();
//        //课程时间
//        Integer courseTime = tCourseInformation.getCourseTime();
//        //课程结束时间
//        String courseEndTime = TimeJudgeUtils.addHourTime(courseStartTime, courseTime);
//        //上课时间
//        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(courseStartTime);
//        //结课时间
//        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(courseEndTime);
//
//        Date date = new Date();
//        String courseStatus=tCourseInformationService.setDate(date,startDate,endDate);
//
//        tCourseInformation.setCourseStatus(courseStatus);
        tCourseInformationService.save(tCourseInformation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TCourseInformationEntity tCourseInformation) throws ParseException {
//        //课程开始时间
//        String courseStartTime = tCourseInformation.getCourseStartTime();
//        //课程时间
//        Integer courseTime = tCourseInformation.getCourseTime();
//        //课程结束时间
//        String courseEndTime = TimeJudgeUtils.addHourTime(courseStartTime, courseTime);
//        //上课时间
//        Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(courseStartTime);
//        //结课时间
//        Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(courseEndTime);
//
//        Date date = new Date();
//        String courseStatus=tCourseInformationService.setDate(date,startDate,endDate);
//
//        tCourseInformation.setCourseStatus(courseStatus);
        tCourseInformationService.updateById(tCourseInformation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        tCourseInformationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/study")
    public R studyCourse(@RequestBody List<TCourseInformationEntity> tCourseInformation){
        return tCourseInformationService.studyCourse(tCourseInformation.get(0));
    }

    @GetMapping("/export")
    public R exportData(HttpServletResponse response){
        return tCourseInformationService.exportData(response);
    }


    @PostMapping("/import")
    public R  importData(@RequestParam("file") MultipartFile file){
        return  tCourseInformationService.importData(file);
    }

}
